import { defineStore } from 'pinia';
import { getUser } from '@/apis/user.js';
const defaultAvatarUrl = import.meta.env.BASE_URL + 'account-list/ic-account-avatar-default.jpg';

export const useUserStore = defineStore('user', {
  state: () => ({
    userInfo: {
      username: '',
      avatarUrl: '' || defaultAvatarUrl,
    },
  }),

  actions: {
    // 设置用户信息
    async setUserState() {
      this.userInfo = {
        username: '',
        avatarUrl: '' || defaultAvatarUrl,
      };

      let userRes = await getUser().catch((err) => err);
      // console.log(userRes, 'userRes');
      if (userRes.code == 0) {
        for (let key in userRes.data) {
          this.userInfo[key] = userRes.data[key];
        }
      }
      return userRes;
    },
  },
});
